package org.angelica.utils.common;

import org.angelica.utils.json.JacksonUtil;
import org.apache.commons.codec.digest.DigestUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 项目常用工具类
 * @author aizhimin
 *
 */
public class ToolUtil {
	
	/**
	 * 获取异常堆栈信息
	 * @param e 异常
	 * @return String
	 */
	public static String getExceptionMsg(Exception e) {  
		  
        StringBuffer sb = new StringBuffer();  
        sb.append(e.toString()+"\n");
        StackTraceElement[] stackArray = e.getStackTrace();  
        for (int i = 0; i < stackArray.length; i++) {  
            StackTraceElement element = stackArray[i];  
            sb.append("	at "+element.toString() + "\n");
        }  
        return sb.toString();  
    }

	/**
	 * 获取异常堆栈信息
	 * @param e 异常
	 * @return String
	 */
	public static String getExceptionMsg(Throwable e) {

		StringBuffer sb = new StringBuffer();
		sb.append(e.toString()+"\n");
		StackTraceElement[] stackArray = e.getStackTrace();
		for (int i = 0; i < stackArray.length; i++) {
			StackTraceElement element = stackArray[i];
			sb.append("	at "+element.toString() + "\n");
		}
		return sb.toString();
	}

	/**
	 * 请求参数签名方法
	 * @param paramMap
	 * @return
	 */
	public static String sign(String appid,String appSecret,Map<String,String> paramMap) {
		//对参数名进行字典排序
		String[] keyArray = paramMap.keySet().toArray(new String[0]);
		Arrays.sort(keyArray);
		
		//拼接有序的参数-参数值
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(appid);
		for(String key:keyArray) {
			stringBuilder.append(key).append(paramMap.get(key));
		}
		stringBuilder.append(appSecret);
		String codes = stringBuilder.toString();
		return DigestUtils.md5Hex(codes).toLowerCase();
	}

	/**
	 * 响应字符串
	 * @param hsResponse
	 * @param responseResult
	 * @throws IOException
	 */
	public static void responseError(HttpServletResponse hsResponse, Map<String,Object> responseResult) throws IOException {
		hsResponse.setContentType("application/json;charset=utf-8");
		String json = JacksonUtil.nonDefaultMapper().toJson(responseResult);
		hsResponse.getWriter().print(json);
		hsResponse.getWriter().close();
	}

	/**
	 * 分割list集合
	 *
	 * @param source
	 * @param n
	 * @return
	 */
	public static <T> List<List<T>> splitList(List<T> source, int n) {
		List<List<T>> result = new ArrayList<List<T>>();

		if (n == 0) {
			result.add(source);
			return result;
		}

		// (先计算出余数)
		int remaider = source.size() % n;
		// 然后是商
		int number = source.size() / n;
		// 偏移量
		int offset = 0;
		for (int i = 0; i < n; i++) {
			List<T> value = null;
			if (remaider > 0) {
				value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
				remaider--;
				offset++;
			} else {
				value = source.subList(i * number + offset, (i + 1) * number + offset);
			}
			result.add(value);
		}
		return result;
	}



//	public static void main(String[] args) {
//		Map<String,String> paramMap = new HashMap<>();
//		Map<String,String> headerMap = ToolUtil.buildRequestHeader("24634645657", "dlxi*^(*&hashfwr65624123pi", paramMap);
//		System.out.println(headerMap.toString());
//		String codes = "24634645657avatarhttps://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKO5BHFN0sGeTySPo3ib2SUJmLpklVaQuj274F1ccwIS37BdB5lMbMtyiaiczFHdJQceiamOZibWBFTYvQ/132gender1nickname艾志敏nonce98eb6e7a505142ba033005b6f36abb94timestamp1532585778338dlxi*^(*&hashfwr65624123pi";
//		System.out.println(DigestUtils.md5Hex(codes).toLowerCase());


//	}
}
